package com.helloae.chap07;

import java.io.File;

import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.FieldChecker;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.IEnumInvalidObject;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClassProxy;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.GeographicCoordinateSystem;
import com.esri.arcgis.geometry.IGeoTransformation;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRGeoTransformation2Type;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 地理坐标转换，从nad1927转换到nad1983<br>
 * 示例章节：7.5.2
 * @author light
 *
 */
public class NAD27to83_Shapefile {

	public static void main(String[] args) throws Exception {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		WorkspaceName pInWSName = new WorkspaceName();
		pInWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");

		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		pInWSName.setPathName(ecliseWorkspace + "HelloAE" + File.separator + "DataSet" +File.separator + "chap07");

		FeatureClassName pInFCName = new FeatureClassName();
		pInFCName.setName("idll27");
		pInFCName.setWorkspaceNameByRef(pInWSName);
		IFeatureClass pInFC = new IFeatureClassProxy(pInFCName.open());

		//生成用于转换的GeographicCoordinateSystem
		SpatialReferenceEnvironment pSpatRefFact = new SpatialReferenceEnvironment();
		IGeoTransformation pGeotransNAD27toNAD83 = (IGeoTransformation) pSpatRefFact
				.createGeoTransformation(esriSRGeoTransformation2Type.esriSRGeoTransformation_NAD_1927_TO_NAD_1983_NADCON);
		GeographicCoordinateSystem[] pFromGCS = new GeographicCoordinateSystem[1];
		GeographicCoordinateSystem[] pToGCS = new GeographicCoordinateSystem[1];
		pGeotransNAD27toNAD83.getSpatialReferences(pFromGCS, pToGCS);
		System.out.println("该程序将进行" + pFromGCS[0].getName() + "到"	+ pToGCS[0].getName() + "的转换。");
		
		//设置输出文件的路径及名称
		WorkspaceName pOutWSName = new WorkspaceName();
		pOutWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		pOutWSName.setPathName("c:\\temp");
		FeatureClassName pOutFCName = new FeatureClassName();
		pOutFCName.setName("idll83");
		pOutFCName.setWorkspaceNameByRef(pOutWSName);
		
		//指定输出shp文件所保留的原有字段
		IFields pInFCFields = pInFC.getFields();
		Fields pOutFCFields[] = new Fields[1];
		FieldChecker pFieldCheck = new FieldChecker();
		pFieldCheck.validate(pInFCFields, null, pOutFCFields);
		
		IField pGeoField = new Field(); 
		for(int i = 0; i < pOutFCFields[0].getFieldCount() - 1; i++	){
			if(pOutFCFields[0].getField(i).getType() == esriFieldType.esriFieldTypeGeometry){
				pGeoField = pOutFCFields[0].getField(i);
				break;
			}
		}
		//IGeometryDefEdit用于创建一个空间索引和空间参考
		IGeometryDefEdit pOutFCGeoDefEdit = (IGeometryDefEdit) pGeoField.getGeometryDef();
		pOutFCGeoDefEdit.setGridCount(1);
		pOutFCGeoDefEdit.setGridSize(0, 200);
		pOutFCGeoDefEdit.setSpatialReferenceByRef((ISpatialReference)pToGCS[0]);
		
		//转换
		FeatureDataConverter pFDConverter = new FeatureDataConverter();
		pFDConverter.convertFeatureClass(pInFCName, null, null, pOutFCName, pOutFCGeoDefEdit, pInFCFields, "", 1000, 0);
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
